<%@ page language="java" contentType="text/html; charset=UTF-8" 
    pageEncoding="UTF-8"%>
<body>

    <div style="margin:10px 0;"></div>
    <table id="list"></table>
    <script type="text/javascript">
(function(ResourceAuthJs, $, undefined ) { 
	
    var fieldData = "";
    var fieldIndex = "";
	var resource = <%=request.getParameter("id")%>;
	$.extend($.fn.datagrid.methods, {
        editCell: function(jq,param){
            return jq.each(function(){
                var opts = $(this).datagrid('options');
                var fields = $(this).datagrid('getColumnFields',true).concat($(this).datagrid('getColumnFields'));
                for(var i=0; i<fields.length; i++){
                    var col = $(this).datagrid('getColumnOption', fields[i]);
                    col.editor1 = col.editor;
                    if (fields[i] != param.field){
                        col.editor = null;
                    }
                }
                $(this).datagrid('beginEdit', param.index);
                for(var i=0; i<fields.length; i++){
                    var col = $(this).datagrid('getColumnOption', fields[i]);
                    col.editor = col.editor1;
                }
            });
        }
    });
    
    var editIndex = undefined;
    function endEditing(){
        if (editIndex == undefined){return true}
        if ($('#list').datagrid('validateRow', editIndex)){
            $('#list').datagrid('endEdit', editIndex);
            editIndex = undefined;
            
            var rowData = $('#list').datagrid('getSelected');
			if(rowData[fieldIndex] != fieldData)
			{
				var url = "dispatch/utility/folder/auth/update?role="+rowData.role+"&folder="+resource+"&action="+fieldIndex+"&enabled="+rowData[fieldIndex];
				//alert(url);
				workbenchJs.loadData(url,ResourceAuthJs.updateAuthResult,"更新文件夹权限");
			}
			
            return true;
        } else {
            return false;
        }
    }
    function onClickCell(index, field){
        if (endEditing()){
            $('#list').datagrid('selectRow', index)
                    .datagrid('editCell', {index:index,field:field});
            editIndex = index;
            var row = $('#list').datagrid('getSelected');
            fieldData = row[field];
            fieldIndex = field;
            if(fieldData == null)fieldData = '';
        }
    }
    
	$('#list').datagrid({
		title:'文件夹权限管理',
		height:400,
		fitColumns:true,
		singleSelect:true,
		url:null,
		frozenColumns:[[    
						{title:'角色',field:'roleName',align:"center",width:80}, 
	                ]],  
	    columns:[
	             [{title:"权限列表(选中为允许 否则为不允许)",align:"center",colspan:3}],
	             [             	
	             	{title:'查看',field:'view',width:30,align:"center",
	             		editor:{type:'checkbox',options:{on:'true',off:'',onclick:function(){alert('sss')}}},
	             		formatter:checkBoxFormat
	             	},
	             	{title:'添加子文件',field:'addFile',width:30,align:"center",
	             		editor:{type:'checkbox',options:{on:'true',off:''}},
	             		formatter:checkBoxFormat
	             	}
	             ]
	    		],
		pagination:false,
		onClickCell: onClickCell,

	});
	
	function checkBoxFormat(value){
        if (value == 'true') {  
            return '允许';  
        }  
        else {
        	return '不允许';
        } 
	}
	ResourceAuthJs.init = function(){
		
		workbenchJs.loadData("dispatch/utility/folder/auth/query?id="+resource,ResourceAuthJs.loadItemData,"查询文件夹权限");
	}
	
	function loadData(url,func){
		$.ajax({
            type: "post",
            url: url,
            dataType:"json",
            success: function (result) {
            	func(result);
            }
            
        });
	}
	ResourceAuthJs.updateAuthResult = function(result){
    	var res = result.result;
        if(res == "SUCCESS")
        {
        }else if(res == "COMPONENT_ERROR")
        {
        	$.messager.alert("Error",result.payload.value);
        }else
        {
        	$.messager.alert("Error",result.result);
        }
	};
	ResourceAuthJs.loadItemData = function(result){
    	var res = result.result;
        if(res == "SUCCESS")
        {
        	var payload = jQuery.parseJSON(result.payload.value);
        	$('#list').datagrid('loadData',payload);
        }else if(res == "COMPONENT_ERROR")
        {
        	$.messager.alert("Error",result.payload.value);
        }else
        {
        	$.messager.alert("Error",result.result);
        }
	};
	
}( window.ResourceAuthJs = window.ResourceAuthJs || {}, jQuery ));

$(function(){
	ResourceAuthJs.init();
});
    </script>
    <style type="text/css">
        #fm{
            margin:0;
            padding:10px 30px;
        }
        .ftitle{
            font-size:14px;
            font-weight:bold;
            padding:5px 0;
            margin-bottom:10px;
            border-bottom:1px solid #ccc;
        }
        .fitem{
            margin-bottom:5px;
        }
        .fitem label{
            display:inline-block;
            width:80px;
            float:left;
        }
        .view{
        	margin-bottom:5px;
        	margin-left:9px;
        	display:inline-block;
            width:200px;
        }
    </style>
</body>